package com.unproy.model.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;


@Entity
@Table(name = "TBROLE")
public class Role implements Serializable, Comparable<Role> {
	private static final long	serialVersionUID	= -339808363205883337L;
	private Short				roleId;
	private String				description;
	private Set<User>			users				= new HashSet<User>();
	
	@Id
	@SequenceGenerator(name = "roleId", sequenceName = "SQROLE")
	@GeneratedValue(generator = "roleId", strategy = GenerationType.SEQUENCE)
	@Column(name = "ROLEID", unique = true, nullable = false)
	public Short getRoleId() {
		return this.roleId;
	}
	
	public void setRoleId(Short roleId) {
		this.roleId = roleId;
	}
	
	@Column(name = "DSCRPT", nullable = false)
	public String getDescription() {
		return this.description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
	public Set<User> getUsers() {
		return this.users;
	}
	
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((this.description == null) ? 0 : this.description.hashCode());
		result = prime * result + ((this.roleId == null) ? 0 : this.roleId.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (this.description == null) {
			if (other.description != null)
				return false;
		} else if (!this.description.equals(other.description))
			return false;
		if (this.roleId == null) {
			if (other.roleId != null)
				return false;
		} else if (!this.roleId.equals(other.roleId))
			return false;
		return true;
	}
	
	@Override
	public int compareTo(Role role) {
		return getDescription().compareTo(role.getDescription());
	}
	
	@Override
	public String toString() {
		return getDescription();
	}
}
